/**
 * 
 */
package com.jolestar.yayaweibotong.task;

import java.util.Date;
import java.util.Map;
import java.util.logging.Level;

import org.apache.commons.lang.StringUtils;

import com.google.apphosting.api.DeadlineExceededException;
import com.jolestar.yayaweibotong.ApiReachLimitException;
import com.jolestar.yayaweibotong.NetworkException;
import com.jolestar.yayaweibotong.SkipCurrentStatusException;
import com.jolestar.yayaweibotong.YaYaConstants;
import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.StatusDomain;
import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.service.StatusService;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 * 
 */
public class UpdateStatusTask extends AbstractTask {

	public static final String QUENE_NAME = "update-status-queue";

	/**
	 * @param name
	 */
	public UpdateStatusTask() {
		super(QUENE_NAME);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jolestar.yayaweibotong.task.AbstractTask#doTask(java.util.Map)
	 */
	@Override
	protected boolean doTask(Map<String, String> headers,
			Map<String, String> params) {
		String accountKeyName = params.get("account");
		String statusKeyName = params.get("status");
		if (StringUtils.isBlank(accountKeyName)) {
			return true;
		}
		if (StringUtils.isBlank(statusKeyName)) {
			return true;
		}
		AccountDomain account = UserService.ServiceHolder.getInstance()
				.getAccount(accountKeyName);
		if (account == null) {
			log.warning("can not find AccountDomain with keyName:"
					+ accountKeyName);
			return true;
		}
		StatusDomain status = UserService.ServiceHolder.getInstance()
				.getStatus(statusKeyName);
		if (status == null) {
			log.warning("can not find StatusDomain with keyName:"
					+ statusKeyName);
			return true;
		}
		UserDomain user = account.getUser();
		StatusService statusService = account.getServiceType()
				.getStatusService();
		try {
			statusService.updateStatus(account, status);
			account.setError("");
		} catch (DeadlineExceededException e) {
			log.warning("更新微博超时:" + e.getMessage());
			return false;
		} catch (SkipCurrentStatusException e) {
			log.log(Level.SEVERE, "更新微博失败。用户:" + user.getEmail() + ",account:"
					+ account.getKeyName() + ", token:" + account.getToken()
					+ ",tokenSecret:" + account.getTokenSecret());
			log.log(Level.SEVERE, "skip current status:" + status.getText());
			log.log(Level.SEVERE, e.getMessage(), e);
			status.setSynced(true);
			status.setSyncedAt(new Date());
			status.save();
			return true;
		} catch (NetworkException e) {
			log.warning("netword error:" + e.getMessage() + ",retry latter.");
			this.retryLatter(headers, params, YaYaConstants.ONE_MINUTE);
			return true;
		} catch (ApiReachLimitException e) {
			log.warning(e.getMessage() + ",suspend account,and retry latter.");
			account.suspendTo(new Date(System.currentTimeMillis()
					+ YaYaConstants.TEN_MINUTE));
			account.save();
			this.retryLatter(headers, params, YaYaConstants.TEN_MINUTE);
			return true;
		} catch (Exception e) {
			log.log(Level.SEVERE, "更新微博失败。用户:" + user.getEmail() + ",account:"
					+ account.getKeyName() + ", token:" + account.getToken()
					+ ",tokenSecret:" + account.getTokenSecret());
			log.log(Level.SEVERE, e.getMessage(), e);
			account.setError(e.getMessage());
			account.save();
			return false;
		}
		status.setSynced(true);
		status.setSyncedAt(new Date());
		status.save();
		return true;
	}

}
